using System;
using XLua.LuaDLL;

namespace XLua.CSObjectWrap
{
	public class CSCallLuaItfDBridge : LuaBase, CSCallLua.ItfD
	{
		int CSCallLua.ItfD.f1
		{
			get
			{
				IntPtr l = luaEnv.L;
				int oldTop = Lua.lua_gettop(l);
				Lua.lua_getref(l, luaReference);
				Lua.xlua_pushasciistring(l, "f1");
				if (Lua.xlua_pgettable(l, -2) != 0)
				{
					luaEnv.ThrowExceptionFromError(oldTop);
				}
				int result = Lua.xlua_tointeger(l, -1);
				Lua.lua_pop(l, 2);
				return result;
			}
			set
			{
				IntPtr l = luaEnv.L;
				int oldTop = Lua.lua_gettop(l);
				Lua.lua_getref(l, luaReference);
				Lua.xlua_pushasciistring(l, "f1");
				Lua.xlua_pushinteger(l, value);
				if (Lua.xlua_psettable(l, -3) != 0)
				{
					luaEnv.ThrowExceptionFromError(oldTop);
				}
				Lua.lua_pop(l, 1);
			}
		}

		int CSCallLua.ItfD.f2
		{
			get
			{
				IntPtr l = luaEnv.L;
				int oldTop = Lua.lua_gettop(l);
				Lua.lua_getref(l, luaReference);
				Lua.xlua_pushasciistring(l, "f2");
				if (Lua.xlua_pgettable(l, -2) != 0)
				{
					luaEnv.ThrowExceptionFromError(oldTop);
				}
				int result = Lua.xlua_tointeger(l, -1);
				Lua.lua_pop(l, 2);
				return result;
			}
			set
			{
				IntPtr l = luaEnv.L;
				int oldTop = Lua.lua_gettop(l);
				Lua.lua_getref(l, luaReference);
				Lua.xlua_pushasciistring(l, "f2");
				Lua.xlua_pushinteger(l, value);
				if (Lua.xlua_psettable(l, -3) != 0)
				{
					luaEnv.ThrowExceptionFromError(oldTop);
				}
				Lua.lua_pop(l, 1);
			}
		}

		public static LuaBase __Create(int reference, LuaEnv luaenv)
		{
			return new CSCallLuaItfDBridge(reference, luaenv);
		}

		public CSCallLuaItfDBridge(int reference, LuaEnv luaenv)
			: base(reference, luaenv)
		{
		}

		int CSCallLua.ItfD.add(int a, int b)
		{
			IntPtr l = luaEnv.L;
			int num = Lua.load_error_func(l, luaEnv.errorFuncRef);
			Lua.lua_getref(l, luaReference);
			Lua.xlua_pushasciistring(l, "add");
			if (Lua.xlua_pgettable(l, -2) != 0)
			{
				luaEnv.ThrowExceptionFromError(num - 1);
			}
			if (!Lua.lua_isfunction(l, -1))
			{
				Lua.xlua_pushasciistring(l, "no such function add");
				luaEnv.ThrowExceptionFromError(num - 1);
			}
			Lua.lua_pushvalue(l, -2);
			Lua.lua_remove(l, -3);
			Lua.xlua_pushinteger(l, a);
			Lua.xlua_pushinteger(l, b);
			if (Lua.lua_pcall(l, 3, 1, num) != 0)
			{
				luaEnv.ThrowExceptionFromError(num - 1);
			}
			int result = Lua.xlua_tointeger(l, num + 1);
			Lua.lua_settop(l, num - 1);
			return result;
		}
	}
}
